<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto">
			<el-card shadow="hover" class="pageQueryBoxArea mb10">
				<el-form :model="state.param" :inline="true">
					<el-form-item label="模板名称：">
						<el-input placeholder="请输入模板名称" clearable></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" >
							<el-icon>
								<ele-Search />
							</el-icon> 搜 索
						</el-button>
						<el-button  type="success" class="ml20" @click="addTempLate('add')">
						<el-icon>
							<ele-FolderAdd />
						</el-icon>
							新增
					</el-button>
					</el-form-item>
				</el-form>
			</el-card>
			<el-card shadow="hover" style="display: flex;flex-direction: column;flex: 1;height: inherit;">
				<section style="display: flex;flex-direction: column; flex: 1;height: inherit;">
					<el-table :data="state.tableData.data" style="width: 100%">
						<el-table-column prop="id" label="模板ID" show-overflow-tooltip ></el-table-column>
						<el-table-column prop="name" label="模板名称" show-overflow-tooltip></el-table-column>
						<el-table-column prop="id" label="模板类型" show-overflow-tooltip></el-table-column>
						<el-table-column prop="id" label="创建时间" >
							<template  #default="scope">
								<div>
									{{ formatDate(new Date(scope.row.createTime), 'YYYY-mm-dd HH:MM:SS') }}
								</div>
							</template>
						</el-table-column>
						<el-table-column prop="id" label="编辑时间">
							<template  #default="scope">
								<div>
									{{ formatDate(new Date(scope.row.updateTime), 'YYYY-mm-dd HH:MM:SS') }}
								</div>
							</template>
						</el-table-column>
						<el-table-column label="操作">
							<template #default="scope">
								<span style="color:var(--el-color-primary);" 
									@click="addTempLate('edit',scope.row)"> 编辑 </span>
								<span style="color:var(--el-color-primary);">&nbsp;|&nbsp;</span>
								<span style="color:var(--el-color-primary);" 
									@click="deleteHandle(scope.$index,scope.row)"> 删除 </span>
							</template>
						</el-table-column>
					</el-table>
					<el-row justify="end">
						<el-col :span="24" class="flex-end-center">
							<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
								:pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum" background
								v-model:page-size="state.tableData.param.pageSize" layout="total, sizes, prev, pager, next, jumper"
								:total="state.tableData.total">
							</el-pagination>
						</el-col>
					</el-row>
				</section>
			</el-card>
		</div>
		<freightTemplate ref="freightTemplateRef"></freightTemplate>
	</div>
</template>


<script setup lang="ts">
import { onMounted, reactive, ref, defineAsyncComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { freightTemplateList } from "/@/api/common"
import { formatDate } from '/@/utils/formatTime';
const freightTemplate = defineAsyncComponent(() => import('/@/components/freightTemplate/freightTemplate.vue'))
const freightTemplateRef = ref();
// 添加模板

// 引入组件

// 定义变量内容
const state = reactive({
	param: {
		mode: null, // 门店类型  null(默认) 全部 0 直营  1 加盟
		storeStatus:-1,
		nodeId: null, // 门店id
		dateTime: [],
		startTime: "",// 开始时间
		endTime: "",// 结束时间 
	},
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			search: '',
			pageNum: 1,
			pageSize: 10,
		},
	},
});

// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	const params = {
		pageNum: state.tableData.param.pageNum,
		pageSize: state.tableData.param.pageSize,
		keyWords:''
	}
	const { results } = await freightTemplateList(params)

	state.tableData.data = results.records;
	state.tableData.total = results.total;
	state.tableData.loading = false;
};

// 新增或者编辑模板
const addTempLate = (type: string, row ?: Object) => {
	freightTemplateRef.value.openDialog(type, row);
};

// 删除模板
const deleteHandle = (index: number,row: any) => {
	ElMessageBox.confirm(`此操作将永久删除改运费模板：“${row.roleName}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		ElMessage.success('删除成功');
	}).catch(() => {});
};



// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
<style scoped lang="scss">
@import '../../../theme/mixins/index.scss';
:deep(.el-card__body){
	display: flex;
	flex-direction: column;
	flex: 1;
	height: inherit;
	.el-table{
		flex: 1;
		.el-table__inner-wrapper::before{
			background: none !important;
		}
	}
}
</style>
